package com.netease.financial.data.repository.datasource;

import android.content.Context;

import com.netease.financial.data.cache.NfsCache;
import com.netease.financial.data.net.RestApi;
import com.netease.financial.data.net.RestApiImpl;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * Created by wyf on 10/17/15.
 */
@Singleton
public class NfsDataStoreFactory {

    private final Context context;
    private final NfsCache nfsCache;

    @Inject
    public NfsDataStoreFactory(Context context, NfsCache nfsCache) {
        if (context == null || nfsCache == null) {
            throw new IllegalArgumentException("Constructor parameters cannot be null!!!");
        }
        this.context = context.getApplicationContext();
        this.nfsCache = nfsCache;
    }

    public NfsDataStore createCloudDataStore() {
        RestApi restApi = new RestApiImpl(this.context);
        return new CloudNfsDataStore(restApi, this.nfsCache);
    }

    /**
     * Create {@link NfsDataStore} from a user id.
     */
    public NfsDataStore create() {
        NfsDataStore userDataStore;

        if (!this.nfsCache.isAppUpdateInfoCacheExpired() &&
                this.nfsCache.isAppUpdateInfoCached()) {
            userDataStore = new DiskNfsDataStore(this.nfsCache);
        } else {
            userDataStore = createCloudDataStore();
        }

        return userDataStore;
    }
}
